package com.sll.hosptials.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Method;
@Aspect
@Component
@Slf4j
public class LogAspect {

    @Pointcut("execution(* com.sll.hosptials.service.*.*(..))")
    public void pc() {

    }

    //    注意*和com之间一定要有空格liquibase
    @Around("pc()")
//    @Around("execution(* com.sll.hosptials.service.*.*(..))")
    public Object logAspect(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        方法所属类的类名
//        String className = methodSignature.getDeclaringTypeName();
//        获取当前切点方法对象
        Method method = methodSignature.getMethod();
//        String args = Arrays.toString(joinPoint.getArgs());   // 存在问题，extends的实体类参数不能获取
        Object[] args = joinPoint.getArgs();
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (arg instanceof MultipartFile) {
                MultipartFile file = (MultipartFile) arg;
                // 处理MultipartFile
                jsonObject.set("type", "文件上传");
                jsonObject.set("filename", file.getOriginalFilename());
                jsonObject.set("param", "file");
            } else {
                jsonObject.set("param" + i, arg);
            }
        }
        Object params = ObjectUtil.clone(jsonObject);
//        JSON.toJSON(JSON.toJSONString(arg));
        log.info("入参信息" + "方法：" + method + "参数：" + params);
        Object object = joinPoint.proceed();
        return object;
    }

}
